package com.class2.two;

/**
 * 二维数组的基本使用（难点）
 *
 * @author 尚硅谷-宋红康
 * @create 13:08
 */
public class TwoArrayTest {
    public static void main(String[] args) {
        //1. 数组的声明与初始化
        //复习
        int[] arr1 = new int[2];
        arr1[0] = 1;
        int[] a2 = new int[]{1, 2};
        int[] a3 = {1, 2};
//        方式1：静态初始化：数组变量的赋值和数组元素的赋值同时进行
        int[][] arr2 = new int[][]{{1, 2, 3}, {4, 5}, {6, 7, 8}};
//        方式2：动态初始化1：数组变量的赋值和数组元素的赋值分开进行
        String[][] arr3 = new String[3][4];
        int[] arr7[] = {{1, 2, 3}, {4, 5}, {6, 7, 8}};
//        方式2：动态初始化2
        double[][] arr4 = new double[2][];
//        其他正确写法
        int arr5[][] = new int[][]{{1, 2, 3}, {4, 5}, {6, 7, 8}};
        int[] arr6[] = new int[][]{{1, 2, 3}, {4, 5}, {6, 7, 8}};

//        错误的写法
//        int arr8[][] = new int[8][8]{{1, 2, 3}, {4, 5}, {6, 7, 8}};
//        int arr8[8][8] = new int[][]{{1, 2, 3}, {4, 5}, {6, 7, 8}};
//        double[][] arr8 = new double[][8];

        //2. 数组元素的调用
//        针对arr2来说，外层元素{1, 2, 3}, {4, 5}, {6, 7, 8}
//        内层元素:1,2,3,4,5,6,7,8
//        调用内层元素
        System.out.println(arr2[0][0]);//1
        System.out.println(arr2[1][0]);//4
        System.out.println(arr2[2][1]);//7
//        调用外层元素
        System.out.println(arr2[0]);
//        测试arr3，arr4
        arr3[0][1] = "tom";
        System.out.println(arr3[0][1]);
//        [Ljava.lang.String;@4554617c
        System.out.println(arr3[0]);
//        double[][] arr4 = new double[2][];
//        double[][] arr4 = new double[][]{{0.0,0.0,0.0,0.0},null}
        arr4[0] = new double[4];
        arr4[0][0] = 1.0;
        System.out.println(arr4[0][0]);
        System.out.println(arr4[0][3]);

        //3. 数组的长度
        System.out.println(arr2.length);
        System.out.println(arr2[0].length);
        System.out.println(arr2[1].length);
        System.out.println(arr2[2].length);


        //4. 如何遍历数组
        for (int i = 0; i < arr2.length; i++) {
            for (int j = 0; j < arr2[i].length; j++) {
                System.out.print(arr2[i][j] + "\t");
            }
            System.out.println();
        }


    }
}
